package com.untrance.shop.core.service.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import com.untrance.shop.core.builder.vo.UserDetailsVOBuilder;
import com.untrance.shop.core.service.UserService;
import com.untrance.shop.model.User;

/**
 * @author eugene zadyra
 */
@Service("userDetailsService")
public class UserDetailsServiceBean implements UserDetailsService {
	
	private static Logger log = LoggerFactory.getLogger(UserDetailsServiceBean.class);
	
	@Autowired
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) {
		log.info("Ask access for user: "+username);
		User user = userService.findUserByUsername(username);
		UserDetails userVO = new UserDetailsVOBuilder().createVO(user);
		log.info("User found: "+(userVO==null?"emprty user":userVO.getUsername()));
		return userVO;
	}

}
